package com.magnificent.panda.ui.dialog;

import com.silverhaze.commons.ui.dialogs.DialogConnector;
import com.silverhaze.commons.helpers.CommonUIHelper;
import com.magnificent.panda.model.entity.FlowEvent;
import com.magnificent.panda.model.generator.flow.*;
import com.magnificent.panda.resources.Resources;
import com.magnificent.panda.utils.JFreeChartHelper;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.ArrayList;

/**
 * User: Babanin
 * Date: May 20, 2010
 * Time: 6:10:32 PM
 */
public class FlowGeneratorDialog extends JDialog {
    private DialogConnector<int[]> dialogConnector;

    private JComboBox flowTypeComboBox;

    private JSpinner intensivitySpinner;
    private JSpinner modelingTimeSpinner;

    private JButton saveButton;
    private JButton cancelButton;
    private JButton generateButton;

    private JFreeChart chart;
    private ChartPanel flowChartPanel;

    private int[] flowValues;

    public FlowGeneratorDialog(DialogConnector<int[]> dialogConnector, boolean addValues) {
        this.dialogConnector = dialogConnector;

        initializeControls();
        initializeView();

        if (addValues) {
            saveButton.setText("Добавить");
        } else {
            saveButton.setText("Заменить");
        }
    }

    private void initializeControls() {
        String[] flowTypes = {"пуассоновский", "аддитивный", "с дрожанием", "с дрожанием и пропусками наблюдений"};

        flowTypeComboBox = new JComboBox(flowTypes);

        intensivitySpinner = new JSpinner(new SpinnerNumberModel(10, 1, 100, 1));
        modelingTimeSpinner = new JSpinner(new SpinnerNumberModel(10, 1, 100, 1));

        chart = JFreeChartHelper.newFlowChart(JFreeChartHelper.createFlowDataset(new ArrayList<FlowEvent>()), "", "", "", 100);

        flowChartPanel = new ChartPanel(chart);
        flowChartPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));

        generateButton = new JButton(new AbstractAction("Генерировать") {
            public void actionPerformed(ActionEvent e) {
                int modelingTimer = (Integer) modelingTimeSpinner.getValue();

                AbstractFlow flow = getFlowByName(flowTypeComboBox.getSelectedItem().toString(),
                        (Integer) intensivitySpinner.getValue(), 1, modelingTimer);
                flowValues = flow.generateInt();
                chart.getXYPlot().setDataset(JFreeChartHelper.createFlowDataset(flowValues));
            }
        });

        saveButton = new JButton(new AbstractAction() {
            public void actionPerformed(ActionEvent e) {
                dialogConnector.objectSelected(flowValues);
                dispose();
            }
        });

        cancelButton = new JButton(new AbstractAction("Отмена") {
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });
    }

    private static AbstractFlow getFlowByName(String name, int intensity1, int intensity2, int modellingTime) {
        if ("пуассоновский".equals(name)) {
            return new PuassonFlow(intensity1, intensity2, modellingTime);
        }

        if ("аддитивный".equals(name)) {
            return new AdditiveFlow(intensity1, intensity2, modellingTime);
        }

        if ("с дрожанием".equals(name)) {
            return new VibrationFlow(intensity1, intensity2, modellingTime);
        }

        if ("с дрожанием и пропусками наблюдений".equals(name)) {
            return new VibrationAndObservationOmissionFlow(intensity1, intensity2, modellingTime);
        }

        return null;
    }

    private void initializeView() {
        setModal(true);
        setLayout(new BorderLayout());
        setIconImage(Resources.APP_ICON.getImage());
        setTitle("Panda Modeling Suite - Генерация потока событий");
        getRootPane().setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));


        add(CommonUIHelper.createGridPanel(5, 5, 5, 5, 2, 4,
                "Время моделирования: ", modelingTimeSpinner, CommonUIHelper.hBox(15, "Интенсивность событий: "), intensivitySpinner,
                "Тип генерируемого потока: ", flowTypeComboBox, "", generateButton
        ), BorderLayout.NORTH);

        add(flowChartPanel, BorderLayout.CENTER);

        add(
                CommonUIHelper.packInShiftedPanel(
                        CommonUIHelper.packInPanel(
                                CommonUIHelper.hBox(saveButton, 2, cancelButton), BorderLayout.LINE_END
                        ), 5, 0, 0, 0
                ), BorderLayout.SOUTH
        );

        pack();
        setLocationRelativeTo(null);
    }
}

  /* converted to utf8 */